package com.wargame.gaming_data_center;

import com.wargame.hexmap.HexagonalMap;
import java.util.*;

/**
 * 态势信息分发方法，根据不同的seat分发对应的态势信息
 *
 * @param thisObsv 态势对象
 * @param seat 所属席位
 * @return Object 对应席位的态势信息
 * */
public Object Distribute(Observation thisObsv, String seat) throws Exception {

    double currentTime = thisObsv.getUpdateTime();  // 获取态势信息更新时间（逻辑推演时间
    Map<String, Object> attributes = thisObsv.getAttributes(); // 获取态势信息
    List<HashMap<String,Object>> oprs = (List<HashMap<String,Object>>) attributes.get("Operators"); // 获取算子数据，所有数据格式遵守算子模型
    List<HashMap<String,Object>> actions = (List<HashMap<String,Object>>) attributes.get("Actions"); // 获取动作数据，所有数据格式遵守动作模型
    List<HashMap<String, Object>> targets = (List<HashMap<String,Object>>) attributes.get("Targets"); // 获取目标数据，所有数据格式遵守目标模型
    List<HashMap<String, Object>> judgeInfoes = (List<HashMap<String,Object>>) attributes.get("JudgeInfoes"); // 获取目标数据，所有数据格式遵守目标模型

    HashMap<Integer,List<String>> validActions = (HashMap<Integer,List<String>>) attributes.get("ValidActions");
    HashMap<String,Integer> scores = (HashMap<String,Integer>) attributes.get("Scores");

    HashMap<String, Object> newObsv = new HashMap<>();

    // 分配态势
    if (seat.equals("All")){ // 导演席
        return  attributes;

        } else if(seat.equals("Red")) { // 席位为红方

            List<HashMap<String,Object>> newOprs = new ArrayList<>();
            HashMap<Integer, List<String>> newValidActions = new HashMap<>();

            for (HashMap opr : oprs) {
                // 判断算子是否阵亡
                boolean isDead = (boolean) opr.get("IsDead");
                if (isDead) continue;

                if (opr.get("Color").equals("Red")) {
                    newOprs.add(opr); // 红方算子不用处理直接添加到字典中

                    int oprID = (int) opr.get("ObjID");
                    if (validActions.containsKey(oprID)) newValidActions.put(oprID, validActions.get(oprID)); // 拿到该红方算子的有效动作
                } else {
                    // 设置战争迷雾
                    List<String> attris = new ArrayList<>(Arrays.asList("BasicSpeed","Armor","CarryWeapons","RemainBulletNums",
                                    "ObserveDistance","Speed","MoveToStopRemainTime", "CanToMove","Stop", "MovePath","Blood","MaxBlood","Keep","KeepRemainTime" ,"ChangeStateRemainTime"
                                    ,"WeaponCoolTime","WeaponUnfoldTime", "WeaponUnfoldState","ValidActions"));// 当前版本算子不存在的属性  "LaunchIDs","AliveRemainTime", "GetOnRemainTime", "GetOffRemainTime","TireAccumulateTime", "KeepRemainTime", "Launcher", 

                    for (String attri : attris) {
                        Object o = opr.get(attri);
                        if (o != null) {
                            opr.put(attri, "");
                        }
                    }
                    newOprs.add(opr);
                }
            }

        newObsv.put("Operators", newOprs);
        newObsv.put("ValidActions", newValidActions);
        newObsv.put("Targets", targets);
        newObsv.put("JudgeInfoes", judgeInfoes);
        newObsv.put("Scores", scores);
        newObsv.put("Time", currentTime);

        return newObsv;
    } else if (seat.equals("Blue")) {

        List<HashMap<String,Object>> newOprs = new ArrayList<>();
        HashMap<Integer, List<String>> newValidActions = new HashMap<>();

        for (HashMap<String,Object> opr : oprs) {
            // 判断算子是否阵亡
            boolean isDead = (boolean) opr.get("IsDead");
            if (isDead) continue;

            if (opr.get("Color").equals("Blue")) {
                newOprs.add(opr); // 蓝方算子不用处理直接添加到字典中

                int oprID = (int) opr.get("ObjID");
                if (validActions.containsKey(oprID)) newValidActions.put(oprID, validActions.get(oprID)); // 拿到该红方算子的有效动作
            } else {
                // 设置战争迷雾
                List<String> attris = new ArrayList<>(Arrays.asList("BasicSpeed","Armor","CarryWeapons","RemainBulletNums",
                                    "ObserveDistance","Speed","MoveToStopRemainTime", "CanToMove","Stop", "MovePath","Blood","MaxBlood","Keep","KeepRemainTime" ,"ChangeStateRemainTime"
                                    ,"WeaponCoolTime","WeaponUnfoldTime", "WeaponUnfoldState","ValidActions"));
                for (String attri : attris) {
                    Object o = opr.get(attri);
                    if (o != null) {
                        opr.put(attri, "");
                    }
                }
                newOprs.add(opr);
            }
        }

        newObsv.put("Operators", newOprs);
        newObsv.put("ValidActions", newValidActions);
        newObsv.put("Targets", targets);
        newObsv.put("JudgeInfoes", judgeInfoes);
        newObsv.put("Scores", scores);
        newObsv.put("Time", currentTime);

        return newObsv;
    }else {
        System.out.println("输入的席位名称不支持");
        throw new Exception("输入的席位名称不支持");
        return null;
    }

}
